/*************************************************************************
	> File Name: print_wait.c
	> Author: Angel Criss
	> Blog: blog.abbycin.tk 
	> Created Time: Mon 07 Jul 2014 04:35:27 PM CST
 ************************************************************************/
/*
 *
 * 非异步安全函数
 *
 */

#define _GNU_SOURCE // strsignal will use
#include<sys/wait.h>
#include<stdio.h>
#include<string.h> //strsignal will use

void p_wait_status(char *msg,int status)
{
    if(msg!=NULL)
        printf("%s",msg);

    if(WIFEXITED(status))
    {
        printf("\e[36mchild exited, status = %d\e[0m\n",WEXITSTATUS(status)); // use WEXITSTATUS() get exit status
    }
    else if(WIFSIGNALED(status))
    {
        printf("\e[35mchild killed by signal:\e[0m\e[45m%d\e[0m (%s)\n",WTERMSIG(status),strsignal(WTERMSIG(status)));
        /*
         * 判断是否被信号所杀而退出,再判断该信号是否产生了coredump,
         */
#ifdef WCOREDUMP // not all system support this feature
        if(WCOREDUMP(status))
            printf("\e[42mcore dump\e[0m\n");
#endif
    }
    else if(WIFSTOPPED(status))
    {
        printf("\e[34mchild stopped by singal\e[0m %d (%s)\n",WIFSTOPPED(status),strsignal(WIFSTOPPED(status)));

        /*
         * 再判断如果是被SIGSTOP停止后,是否被SIGCONT唤醒
         */
#ifdef WIFCONTINUED // NOT ALL SYSTEM SUPPORT THIS FEATURE
        if(WIFCONTINUED(status))
            printf("\e[33mchild continued\e[0m\n");
#endif
    }
    else
    {
        printf("\e[32mDon't know what happened to child :(\e[0m\n\t(status = %x)\n",(unsigned int)status);
    }
}
